// miniprogram/pages/admin/schedule/schedule.js
const app = getApp();
const util = require('../../utils/util.js');
const DATA_STYLE = 'background-color: #0f0;color: #fff;';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // now: '2020-10-19',
    // startDate: '2020-10-01',
    // endDate: '2020-12-31',
    calenderItems: [
      // { date: '2020-10-19', style: 'background-color: #0f0;color: #fff;', tip: '3单' },
      // { date: '2020-10-22', style: 'background-color: #666; color: #fff', tip: '5单' },
     ],
     agenda: [
      //  {_id: 1, date: '2020-10-19', time: '09:00', customer: '张三', phone: '15823282821', serice: '甜蜜瞬间', remark: '两套服装分别穿'},
      //  {_id: 2, date: '2020-10-19', time: '15:00', customer: '李四', phone: '15823282821', serice: '甜蜜瞬间', remark: '两套服装分别穿'},
      //  {_id: 3, date: '2020-10-19', time: '17:00', customer: '王王', phone: '15823282821', serice: '甜蜜瞬间', remark: '两套服装分别穿'},
      //  {_id: 4, date: '2020-10-22', time: '08:30', customer: '小明', phone: '15823282821', service: '甜蜜瞬间', remark: '两套服装分别穿'},
      //  {_id: 5, date: '2020-10-22', time: '12:00', customer: '小张', phone: '15823282821', service: '甜蜜瞬间', remark: '两套服装分别穿'},
      //  {_id: 6, date: '2020-10-22', time: '17:00', customer: '王王', phone: '15823282821', service: '甜蜜瞬间', remark: '两套服装分别穿'},
      //  {_id: 7, date: '2020-10-22', time: '09:00', customer: '张三', phone: '15823282821', service: '甜蜜瞬间', remark: '两套服装分别穿'},
      //  {_id: 8, date: '2020-10-22', time: '15:00', customer: '李四', phone: '15823282821', service: '甜蜜瞬间', remark: '两套服装分别穿'},
     ]
  },

  //日期控件点击事件
  onItemClick(e) {
    const that = this;
    let item = this.data.calenderItems.find(item => {
      return (item.date == e.detail.value)
    });
    if (item){
      wx.cloud.callFunction({
        name: 'crud',
        data: {
          op: 'get',
          table: 'order',
          where: {agenda: {date: item.date}, studioid: app.globalData.user.studioid}
        },
        success:res=>{
          // console.log(res);
          that.setData({
            date:item.date,
            agendaOnDay: res.result.data
          }); 
        }
      })
    }else{
      this.setData({date: e.detail.value, agendaOnDay: []});
    }
    // console.log(e.detail)
    // //兼容IOS
    // let chooseDate = (new Date(e.detail.value + " 00:00:00").getTime()) || (new Date(e.detail.value.replace(/-/g, '/') + " 00:00:00").getTime())
    // if(e.detail.value.length < 8) {
    //   chooseDate = (new Date(e.detail.value + "-01 00:00:00").getTime()) || (new Date(e.detail.value.replace(/-/g, '/') + "/01 00:00:00").getTime())
    // }
    // // console.log(chooseDate);
    // let agendaOnDay = [];
    // let agenda = this.data.agenda;
    // for(var i=0; i<agenda.length; i++){
    //   if (agenda[i].date == e.detail.value){
    //     agendaOnDay.push(agenda[i]);
    //   }
    // }
    // this.setData({agendaOnDay});

  },

  makeCall: function(e){
    // 拨打电话
    console.log(e);
    let phone = e.target.dataset.value;
    if (phone){
      wx.makePhoneCall({
        phoneNumber: phone
      }).then(res=>{
        console.log(res);
        
      }).catch(e=>{
        
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let data = this.data;
    let now = new Date();
    data.now = util.formatTime(now).substr(0, 10);
    now.setDate(1);
    now.setMonth(now.getMonth() - 1);
    data.startDate = util.formatTime(now).substr(0, 10);
    now.setMonth(now.getMonth() + 5);
    now.setDate(now.getDate() - 1);
    data.endDate = util.formatTime(now).substr(0, 10);
    this.setData(data);
    app.login(user=>{
      if (!user.studioid) {
        wx.navigateBack();
        return;
      }
      // TODO 云函数需要重新编写，根据日期获取本月的预约数据
      wx.cloud.callFunction({
        name: 'agenda',
        data:{
          op: 'get',
          month: data.now.substr(0,7),
          studioid: user.studioid
        },
        success: res=>{
          console.log(res);
          let agenda = res.result.list;
          // console.log(agenda);
          // TODO 把日历数据映射到 calenderItems 里去，需要进行统计
          // TODO 默认显示当天的订单
          // data.agenda = agenda;  TODO 从数据库中查询
          // data.agendaOnDay = data.agenda;
          let calenderItems = [];
          agenda.map((v,i)=>{
            calenderItems.push({date: v._id, tip: `${v.num}单`, class: 'item-hasOrder-day'});
          });
          
          // TODO 如果数据超过20条，需要两次查询?
          this.setData({calenderItems});
        }
      })
      
    });
  },

  // TODO 切换日历事件，需要重新获取本月份数据
  onDateChange: function(e){
    console.log(e.detail);
    wx.cloud.callFunction({
      name: 'agenda',
      data:{
        op: 'get',
        month: e.detail.value.substr(0,7),
        studioid: app.globalData.user.studioid
      },
      success: res=>{
        console.log(res);
        let agenda = res.result.list;
        // console.log(agenda);
        // TODO 把日历数据映射到 calenderItems 里去，需要进行统计
        // TODO 默认显示当天的订单
        // data.agenda = agenda;  TODO 从数据库中查询
        // data.agendaOnDay = data.agenda;
        let calenderItems = [];
        agenda.map((v,i)=>{
          calenderItems.push({date: v._id, tip: `${v.num}单`, class: 'item-hasOrder-day'});
        });
        
        // TODO 如果数据超过20条，需要两次查询?
        this.setData({calenderItems});
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  dayClick: function (event) {
    wx.navigateTo({
      url: 'newSchedule/newSchedule',
    })
    console.log(event.detail);
},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})